package mutsumimod.cards.copy;

import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import mutsumimod.cards.BaseCard;
import mutsumimod.character.Mutsumi;
import mutsumimod.util.CardStats;
import mutsumimod.util.tags.CustomTags;

import java.util.ArrayList;

public class Megaphone extends BaseCard {
    public static final String ID = makeID(Megaphone.class.getSimpleName());
    public static final CardStats info = new CardStats(
            Mutsumi.Meta.CARD_COLOR,
            CardType.SKILL,
            CardRarity.UNCOMMON,
            CardTarget.SELF,
            1
    );

    public Megaphone() {
        super(ID, info);
        setAttr(0,0,3);
        setAttrUpg(0,0,0);
        originShouldCopyUse = true;
        setShouldCopyUse(true);
        tags.add(CustomTags.SELF_CARD);
    }

    @Override
    public void beforeCopy(){
        clearCopyCards();
        addCopyCards(getTopCards(AbstractDungeon.player.drawPile,magicNumber));
    }

    @Override
    public void afterCopyUse(){
        upgradeBaseCost(++baseCost);
    }

    public ArrayList<AbstractCard> getTopCards(CardGroup group, int count) {
        ArrayList<AbstractCard> result = new ArrayList<>();
        if (group == null || group.group.isEmpty()) {
            return result;
        }

        ArrayList<AbstractCard> filteredDeck = new ArrayList<>(group.group);
        filteredDeck.removeIf(c ->
                c.hasTag(CustomTags.SELF_CARD) ||
                        c.type == AbstractCard.CardType.STATUS ||
                        c.type == AbstractCard.CardType.CURSE
        );

        int endIndex = Math.min(count, filteredDeck.size());
        for (int i = 0; i < endIndex; i++) {
            result.add(filteredDeck.get(i));
        }

        return result;
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public AbstractCard makeCopy() {
        return new Megaphone();
    }

    @Override
    public void upgrade() {
        if(!upgraded){
            upgradeName();
            upgradeBaseCost(--baseCost);
        }
    }
}